const BloomFilter = require("../template")

function testBloomFilterBasic() {
  const bf = new BloomFilter(100, 0.01)

  console.log(
    "1. 基本添加和查询测试:",
    (() => {
      bf.add("hello")
      bf.add("world")
      bf.add("test")
      return bf.contains("hello") === true &&
             bf.contains("world") === true &&
             bf.contains("test") === true &&
             bf.contains("nonexistent") === false
    })()
  )

  console.log(
    "2. 元素计数测试:",
    (() => {
      return bf.getItemCount() === 3
    })()
  )

  console.log(
    "3. 位数组大小测试:",
    (() => {
      return bf.getBitArraySize() > 0
    })()
  )

  console.log(
    "4. 哈希函数数量测试:",
    (() => {
      return bf.getHashFunctionCount() > 0
    })()
  )

  console.log(
    "5. 假阳性率测试:",
    (() => {
      const rate = bf.getFalsePositiveRate()
      return rate >= 0 && rate <= 1
    })()
  )

  console.log(
    "6. 位数组密度测试:",
    (() => {
      const density = bf.getBitArrayDensity()
      return density >= 0 && density <= 1
    })()
  )

  console.log(
    "7. 估算元素数量测试:",
    (() => {
      const estimated = bf.estimateItemCount()
      return estimated >= 0
    })()
  )

  console.log(
    "8. this上下文布隆过滤器测试:",
    (() => {
      const bfObj = {
        multiplier: 2,
        processBloomFilter: function(items) {
          const bf = new BloomFilter(100, 0.01)
          items.forEach(item => bf.add(item))
          return bf.getItemCount() * this.multiplier
        }
      }
      return bfObj.processBloomFilter(["a", "b", "c"]) === 6 // 3*2=6
    })()
  )

  console.log(
    "9. 复杂对象this绑定布隆过滤器测试:",
    (() => {
      const bfConfigObj = {
        config: { threshold: 0.1 },
        processWithConfig: function(items) {
          const bf = new BloomFilter(100, 0.01)
          items.forEach(item => bf.add(item))
          return bf.getFalsePositiveRate() < this.config.threshold
        }
      }
      return bfConfigObj.processWithConfig(["a", "b", "c"]) === true
    })()
  )

  console.log(
    "10. 布隆过滤器一致性测试:",
    (() => {
      const consistencyBf = new BloomFilter(100, 0.01)
      consistencyBf.add("test")
      const count1 = consistencyBf.getItemCount()
      const contains1 = consistencyBf.contains("test")
      consistencyBf.clear()
      const count2 = consistencyBf.getItemCount()
      const contains2 = consistencyBf.contains("test")
      return count1 === 1 && contains1 === true && count2 === 0 && contains2 === false
    })()
  )

  console.log(
    "11. 布隆过滤器边界测试:",
    (() => {
      const boundaryBf = new BloomFilter(1, 0.01)
      boundaryBf.add("")
      return boundaryBf.contains("") === true &&
             boundaryBf.getItemCount() === 1
    })()
  )

  console.log(
    "12. 布隆过滤器性能测试:",
    (() => {
      const perfBf = new BloomFilter(1000, 0.01)
      const items = Array.from({ length: 100 }, (_, i) => `item${i}`)
      const start = performance.now()
      items.forEach(item => perfBf.add(item))
      items.forEach(item => perfBf.contains(item))
      const end = performance.now()
      return perfBf.getItemCount() === 100 && (end - start) < 1000
    })()
  )
}

testBloomFilterBasic()
